package com.xxx.controller;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xxx.common.Const;
import com.xxx.common.dto.JsonResult;
import com.xxx.common.dto.Page;
import com.xxx.common.dto.Pagination;
import com.xxx.common.utils.ErrorUtil;
import com.xxx.model.FamilyHistory;
import com.xxx.service.FamilyHistoryService;

@Controller
@RequestMapping("familyhistory/*")
public class FamilyHistoryController {
	
	@Autowired
	private FamilyHistoryService familyHistoryService;

	/**
	 * 列表 页面
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "index.html", method = RequestMethod.GET)
	public ModelAndView index(HttpServletRequest request) throws Exception{
		ModelAndView mv = new ModelAndView("familyhistory/index");
		int currentPage = NumberUtils.toInt(request.getParameter("page"), 1);
		Map<String, Object> whereMap = new HashMap<>();
		Page<FamilyHistory> page = familyHistoryService.getPage(whereMap, new Pagination(currentPage));
		mv.addObject("page", page);
		return mv;
	}
	
	/**
	 * 添加/修改 页面
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "create.html", method = RequestMethod.GET)
	public ModelAndView create(HttpServletRequest request) throws Exception{
		ModelAndView mv = new ModelAndView("familyhistory/create");
		long id = NumberUtils.toLong(request.getParameter("id"), Const.DEFAULT_LONG_NULL);
		if(id != Const.DEFAULT_LONG_NULL){
			FamilyHistory familyHistory = familyHistoryService.getEntity(id);
			mv.addObject("data", familyHistory);
		}
		return mv;
	}
	
	/**
	 * 保存/修改
	 * @param FamilyHistory
	 * @param result
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "save", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	public JsonResult save(@Valid FamilyHistory familyHistory, BindingResult result) throws Exception {
		if (result.hasErrors()) {
			return ErrorUtil.getError(result);
		}
		long ret = familyHistoryService.saveOrUpdate(familyHistory);
		if(ret > 0){
			return JsonResult.ok();
		}
		return JsonResult.build(-1, "保存失败!");
	}
	
	/**
	 * 删除
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "delete", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	public JsonResult delete(HttpServletRequest request) throws Exception {
		String[] ids = request.getParameterValues("checkedid");
		if(ids == null || ids.length == 0){
			return JsonResult.build(-1, "请选择要删除的记录!");
		}
		for (String id : ids) {
			familyHistoryService.delete(NumberUtils.toLong(id));
		}
		return  JsonResult.ok();
	}
}
